home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2160 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  80 lines

  1. Path: news.vcd.hp.com!koayst
  2. From: koayst@hpsglmx.sgp.hp.com (Koay Seng Tian)
  3. Newsgroups: comp.lang.c++
  4. Subject: Recursive Definition?
  5. Date: 16 Jan 1996 02:41:55 GMT
  6. Organization: Hewlett-Packard
  7. Message-ID: <4df39j$f80@news.vcd.hp.com>
  8. NNTP-Posting-Host: hpsgl4.sgp.hp.com
  9. X-Newsreader: TIN [version 1.2 021193BETA PL3]
  10.  
  11. I have a problem in understanding the following C++ code which I have gotten
  12. from a book (C++ components and Algorithms by Scott R. Ladd).  Please help!!
  13.  
  14. class Switch()
  15. {
  16.    public:
  17.       Switch();
  18.       Switch(int sw);
  19.       Switch(const Switch & sw);
  20.  
  21.       Switch & operator = (int sw);
  22.       Switch & operator = (const Switch & sw);
  23.  
  24.       static const Switch On;
  25.       static const Switch Off;
  26.  
  27.       int IsOn();
  28.       int IsOff();
  29.  
  30.    protected:
  31.       int setting;
  32. };
  33.  
  34. const Switch Switch::On = 1;
  35. const Switch Switch::Off = 0;
  36.  
  37. What are the above two statements trying to initialize?
  38. It seems to me that they are sort of doing a recursive
  39. initialization.  Please help!!!!
  40.  
  41. Following are the rest of the code.
  42.  
  43. Switch::Switch()
  44. {
  45.    Setting = Off.Setting;
  46. }
  47.  
  48. Switch::Switch(int sw)
  49. {
  50.    if(sw == On.Setting)
  51.       Setting = On.Setting;
  52.    else
  53.       Setting = Off.Setting;
  54. }
  55.  
  56. Switch::Switch(const Switch & sw)
  57. {
  58.    Setting = sw.Setting;
  59. }
  60.  
  61. Switch & Switch::operator = (int sw)
  62. {
  63.    if(sw == On.Setting)
  64.       Setting = On.Setting;
  65.    else
  66.       Setting = Off.Setting;
  67.  
  68.    return *this;
  69. }
  70.  
  71. Switch & Switch::operator = (const Switch & sw)
  72. {
  73.    Setting = sw.Setting;
  74.    return *this;
  75. }
  76.  
  77.  
  78. Seng Tian
  79. Regards
  80.